#include "slaveslot.h"
#include "ui_slaveslot.h"



SlaveSlot::SlaveSlot(QWidget *parent) : QWidget(parent), unitInQueue(0), unitWorked(0), ui(new Ui::SlaveSlot), m_isConnected(false), currentBlinkingLed(0)
{    
    ui->setupUi(this);
    QVBoxLayout *vLay = new QVBoxLayout(this);
    QHBoxLayout *hLayStatusActivity = new QHBoxLayout(this);
    QHBoxLayout *hLayStatusConnection = new QHBoxLayout(this);
    QHBoxLayout *hLayInfoSocket = new QHBoxLayout(this);
    QHBoxLayout *hLayInfoUnitQueue = new QHBoxLayout(this);
    QHBoxLayout *hLayInfoUnitWorked = new QHBoxLayout(this);
    QLabel      *lblStatusActivity = new QLabel(tr("Stato elaborazione: "), this);
    QLabel      *lblConnectionStatus = new QLabel(tr("Stato connessione: "), this);
    QLabel      *lblInfoConnection = new QLabel(tr("Informazioni slave: "), this);
    //
    // objects to GUI:
    hLayInfoUnitQueue->addWidget(new QLabel(tr("Unita' in coda: "), this), 30, Qt::AlignLeft);
    this->m_unitInQueue = new QLabel(tr("0"), this);
    hLayInfoUnitQueue->addWidget(this->m_unitInQueue, Qt::AlignRight);
    hLayInfoUnitWorked->addWidget(new QLabel(tr("Unita' elaborate: "), this), 30, Qt::AlignLeft);
    this->m_unitWorkedFromQueue = new QLabel(tr("0"), this);
    hLayInfoUnitWorked->addWidget(this->m_unitWorkedFromQueue, Qt::AlignRight);
    hLayInfoSocket->addWidget(lblInfoConnection, 0, Qt::AlignLeft);
    this->m_InfoSocket = new QLabel(tr("non connesso"), this);
    hLayInfoSocket->addWidget(m_InfoSocket, 0, Qt::AlignRight);
    this->m_ledStatus = new QLedIndicator(this);        
    hLayStatusActivity->addWidget(lblStatusActivity, 0, Qt::AlignLeft);
    hLayStatusActivity->addWidget(this->m_ledStatus, 0, Qt::AlignRight);
    this->m_ledConnectionStatus = new QLedIndicator(this);
    this->m_ledConnectionStatus->setOffColor1(QColor(Qt::red));
    this->m_ledConnectionStatus->setOffColor2(QColor(Qt::red));
    this->m_ledConnectionStatus->setOnColor1(QColor(Qt::green));
    hLayStatusConnection->addWidget(lblConnectionStatus, 0, Qt::AlignLeft);
    hLayStatusConnection->addWidget(this->m_ledConnectionStatus, 0, Qt::AlignRight);
    this->m_indicator = new QNeedleIndicator(this);
    this->m_indicator->setMinimumSize(200, 200);
    this->m_indicator->setRange(0, kMaxElementInQueue);
    this->m_indicator->setDigitFormat(QString("%.0f"));
    this->m_indicator->setMajorTicks(11);
    this->m_indicator->setMinorTicks(0);
    this->m_indicator->setGapAngle(120);
    this->m_indicator->setAnimated(true);    
    this->m_indicator->setValue(0);
    this->m_indicator->setLabel(tr("Unita' in coda"));
    //
    // layout settings:    
    vLay->addLayout(hLayInfoSocket, 0);
    vLay->addLayout(hLayInfoUnitQueue, 0);
    vLay->addLayout(hLayInfoUnitWorked, 0);
    vLay->addWidget(this->m_indicator);
    vLay->addLayout(hLayStatusConnection, 0);
    vLay->addLayout(hLayStatusActivity, 0);    
    this->setLayout(vLay);
    this->setMinimumSize(this->maximumSize());    
}

void SlaveSlot::acquireLock(void)
{
    this->m_mutex.lock();
}

void SlaveSlot::releaseLock(void)
{
    this->m_mutex.unlock();
}

void SlaveSlot::addWorkedFromQueueLabel()
{
    this->unitWorked++;
    this->m_unitWorkedFromQueue->setText(QString::number(this->unitWorked, 10));
}

void SlaveSlot::updateQueueIndicator()
{
    this->addInQueueLabel(this->unitInQueue);
    if (this->unitInQueue == 0) // elaboration is terminated
        this->setActiveStatus(false);
}

void SlaveSlot::addInQueueLabel(int val)
{
    this->m_unitInQueue->setText(QString::number(val, 10));
    this->m_indicator->setValue(1 * (val % kMaxElementInQueue + 1));
    //QApplication::processEvents();
}

void SlaveSlot::setInfoSocket(QString& info)
{
    this->m_InfoSocket->setText(info);
}

void SlaveSlot::setConnected(bool status)
{
    this->m_ledConnectionStatus->setChecked(status);
    this->m_isConnected = status;
}

void SlaveSlot::resetSlot(void)
{
    QString nonActive = tr("non connesso");

    this->setActiveStatus(false);
    this->setConnected(false);
    this->setInfoSocket(nonActive);
    this->unitInQueue = 0;
    this->unitWorked = 0;    
    this->m_unitInQueue->setText(QString::number(this->unitInQueue, 10));
    this->m_unitWorkedFromQueue->setText(QString::number(this->unitWorked, 10));
}

void SlaveSlot::timerEvent(QTimerEvent *)
{
    if (this->currentBlinkingLed == 0) {
        this->m_ledStatus->setChecked(true);
        this->currentBlinkingLed = 1;
    }
    else {
        this->m_ledStatus->setChecked(false);
        this->currentBlinkingLed = 0;
    }
    //QCoreApplication::processEvents();
}

SlaveSlot::~SlaveSlot()
{
    delete ui;
}

void SlaveSlot::setActiveStatus(bool activity)
{
    if (activity == true)
        this->timerID = this->startTimer(450);
    else {
        this->killTimer(this->timerID);
        this->m_ledStatus->setChecked(false);
    }
}

void SlaveSlot::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}
